#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#define INIT_CAPACITY 4
typedef int SLDataType;
typedef struct SeqList
{
	SLDataType* a;
	int size;     // 有效数据个数
	int capacity; // 空间容量
}SL;

void SLInit(SL* ps)
{
	ps->a = NULL;
	ps->size = 0;
	ps->capacity = 0;
}
void SLDestroy(SL* ps)
{
	assert(ps);
	free(ps->a);//ps->a是动态分配的，一定要释放掉，避免内存泄漏
	ps->a = NULL;
	ps->size = 0;
	ps->capacity = 0;
}
void SLCheckCapacity(SL* ps)
{
	assert(ps);
	if (ps->size == ps->capacity)//这里就涵盖了两种情况：1.列表非空但已满，2.列表为空
	{
		int capacity_tmp;
		if (ps->capacity == 0)
		{
			capacity_tmp = INIT_CAPACITY;
		}
		else
		{
			capacity_tmp = 2 * ps->capacity;
		}
		SLDataType* a_tmp = (SLDataType*)realloc(ps->a, capacity_tmp * sizeof(SLDataType));//避免出现分配失败，原指针丢失的情况
		if (a_tmp)
		{
			ps->a = a_tmp;
			ps->capacity = capacity_tmp;
		}
		else
		{
			perror("申请失败");
			exit(1);
		}
	}
}
void SLPrint(SL* ps)
{
	assert(ps);
	int size = ps->size;
	int i;
	for (i = 0; i < size; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}
void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	ps->a[ps->size] = x;
	ps->size++;
}

void Delete(SL* ps, int item)
{
	int NewSize = 0;
	int i;
	int size = ps->size;
	for (i = 0; i < size; i++)
	{
		if (ps->a[i] != item)
		{
			ps->a[NewSize] = ps->a[i];
			NewSize++;
		}
	}
	ps->size = NewSize;
}

int main()
{
	//先自己生成一组顺序表
	//[7, 4, 10, 8, 2, 4, 6, 1 ,4, 5]
	SL list;
	SLInit(&list);
	SLPushBack(&list, 7);
	SLPushBack(&list, 4);
	SLPushBack(&list, 10);
	SLPushBack(&list, 8);
	SLPushBack(&list, 2);
	SLPushBack(&list, 4);
	SLPushBack(&list, 6);
	SLPushBack(&list, 1);
	SLPushBack(&list, 4);
	SLPushBack(&list, 5);
	Delete(&list, 4);
	SLPrint(&list);
	return 0;
}